package com.wyh.service.serviceImpl.appservieripml;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.common.exception.ResourceNotFoundException;
import com.wyh.domain.dto.gfive.LoginUser;
import com.wyh.domain.po.gfive.User;
import com.wyh.domain.vo.gruopfour.chargeVO.RefundVO;
import com.wyh.domain.vo.gruopfour.threemeterVO.PageResult;
import com.wyh.mapper.appmapper.ChargeMapper;
import com.wyh.service.appservice.ChargeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.wyh.common.enums.ResultCodeEnum.SYSTEM_ERROR;


/**
 * @author : 是你
 * @create : 2024/5/21 下午4:16
 */
@Service
@Slf4j
public class ChargeServiceImpl implements ChargeService {

    @Autowired
    private ChargeMapper chargeMapper;

    private static User getAdminUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            // 获取用户信息
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            return loginUser.getUser();
        } else {
            throw new CustomException(ResultCodeEnum.USER_NOT_LOGIN);
        }
    }

    @Override
    public List<Integer> getAllResourceIds(Integer type,Integer state) {
        //登陆时拿到的管理员的id
        //TODO 登陆时拿到的管理员的id
        Integer id = getAdminUser().getId();

//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//
//        if (authentication != null) {
//            // 获取用户信息
//            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//            System.out.println("==============================================================================");
//            System.out.println(loginUser);
//        }
        List<Integer> layerList =  chargeMapper.getAllLayerIds(id);
        if (layerList != null && !layerList.isEmpty()) {
            return chargeMapper.getAllResourceIds(layerList, type, state);
        }else {
            // layerList为空时的处理逻辑，比如抛出异常、返回默认值或者提示信息
            // 这里以抛出IllegalArgumentException为例
//            throw new ResourceNotFoundException(RESOURCE_NOT_ERROR.getCode(),RESOURCE_NOT_ERROR.getMsg());
            return null;
        }
    }

    @Override
    public List<Integer> getAllResourceIds(Integer state) {
        //登陆时拿到的管理员的id


//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//
//        if (authentication != null) {
//            // 获取用户信息
//            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//            System.out.println("==============================================================================");
//            System.out.println(loginUser);
//        }
        //TODO 登陆时拿到的管理员的id
//        int id = 9;
        Integer id = getAdminUser().getId();
        List<Integer> layerList =  chargeMapper.getAllLayerIds(id);
        int type = -1;
        if (layerList != null && !layerList.isEmpty()) {
            return chargeMapper.getAllResourceIds(layerList, type, state);
        }else {
            // layerList为空时的处理逻辑，比如抛出异常、返回默认值或者提示信息
            // 这里以抛出IllegalArgumentException为例
            return null;
//           throw new ResourceNotFoundException(RESOURCE_NOT_ERROR.getCode(),RESOURCE_NOT_ERROR.getMsg());
        }
    }

    /**
     * 退款列表
     * @param state
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult<RefundVO> getRefund(int state, int pageNum, int pageSize, List<Integer> ids) {
        if(ids.size() == 0){
            throw new ResourceNotFoundException(SYSTEM_ERROR.getCode(),"退款列表不存在");
        }
        List<Integer> customerIds = chargeMapper.getCustomerIds(ids);
        if(customerIds.size() == 0){
            throw new ResourceNotFoundException(SYSTEM_ERROR.getCode(),"退款列表不存在");
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<RefundVO> page = (Page<RefundVO>) chargeMapper.getRefund(customerIds, state);
        List<RefundVO> result = page.getResult();

        // 使用map来转换每个RefundVO对象
        result.forEach(refundVO -> {
            // 将图片地址字符串按逗号分割成列表
            List<String> pictureList = Arrays.asList(refundVO.getPicture().split(","));
            // 假设存在一个方法来设置图片列表
            refundVO.setPictureList(pictureList);
        });


        return new PageResult<>(page.getTotal(),result);
    }

}
